export class AdaptManager {

    public isFullScreenModel: boolean = false;

    private DESIGN_SIZE_WIDTH: number = 1334;
    private DESIGN_SIZE_HEIGHT: number = 750;

    // public static currClientWidth: number = 0;
    // public static currClientHeight: number = 0;

    private static _instance: AdaptManager = null;
    public static get instance(): AdaptManager {
        if (!this._instance) {
            this._instance = new AdaptManager();
            document.addEventListener("fullscreenchange", specialOutFullScreen);
            document.addEventListener("mozfullscreenchange", specialOutFullScreen);
            document.addEventListener("webkitfullscreenchange", specialOutFullScreen);
            document.addEventListener("msfullscreenchange", specialOutFullScreen);
        }
        return this._instance;
    }

    //固定宽高比
    public ratioWHFactor(targetW: number, targetH: number) {
        let factorW: number = targetW / this.DESIGN_SIZE_WIDTH;
        let factorH: number = targetH / this.DESIGN_SIZE_HEIGHT;
        let factor: number = factorW > factorH ? factorH : factorW;
        return { width: factor * this.DESIGN_SIZE_WIDTH, height: factor * this.DESIGN_SIZE_HEIGHT }
    }
}

export function specialOutFullScreen() {

    AdaptManager.instance.isFullScreenModel = ! AdaptManager.instance.isFullScreenModel;

    window.onresize = () => {
        let screenW: number = document.activeElement.clientWidth;
        let screenH: number = document.activeElement.clientHeight;
        let outSize: any = AdaptManager.instance.ratioWHFactor(screenW, screenH);
        let divElement: HTMLElement = document.getElementById('GameCanvas');
        divElement.style.width = outSize.width + 'px';
        divElement.style.height = outSize.height + 'px';
        console.log('adapt: width:' + outSize.width + ' height:' + outSize.height);
    };
}